﻿using System;
using System.Collections.Generic;
//
using PerceptiveMCAPI.Types;
//

namespace PerceptiveMCAPI_Test
{
   public partial class sub_MergeVars : System.Web.UI.UserControl
   {
      public Dictionary<string, object> MergeVarsList
      {
         get { return getMergeVars(); }
      }
      // ----------------------------
      public void Setup()
      {
         this.Visible = true;
         tbMVkey01.Focus();
      }
      // ----------------------------------------------------
      private Dictionary<string, object> getMergeVars()
      {
         Dictionary<string, object> mv = new Dictionary<string, object>();

         if ( tbMVkey01.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey01.Text, tbMVval01.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey02.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey02.Text, tbMVval02.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey03.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey03.Text, tbMVval03.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey04.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey04.Text, tbMVval04.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey05.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey05.Text, tbMVval05.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey06.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey06.Text, tbMVval06.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey07.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey07.Text, tbMVval07.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey08.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey08.Text, tbMVval08.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey09.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey09.Text, tbMVval08.Text );
            mv.Add( entry.Key, entry.Value );
         }
         if ( tbMVkey10.Text != "" )
         {
            KeyValuePair<string, object> entry = fmtMergeVar( tbMVkey10.Text, tbMVval10.Text );
            mv.Add( entry.Key, entry.Value );
         }

         return mv;
      }
      // ----------------------------------------------------------
      private KeyValuePair<string, object> fmtMergeVar( string key_text, string val_text )
      {
         KeyValuePair<string, object> entry = new KeyValuePair<string, object>();
         //
         string key = key_text.Trim();
         object val = val_text.Trim();
         //
         // --- simple string is OK -- except for groupings
         //
         if ( key.ToLower() == "groupings" )
         {
            List<interestGroupings> igList = new List<interestGroupings>();
            /*
             * id=69&name=testfix&groups=yes,maybe
             */
            string[] groupings = val_text.Split( new string[] { "+", "|", ";" }, StringSplitOptions.RemoveEmptyEntries );
            foreach ( string grouping in groupings )
            {
               interestGroupings igEntry = new interestGroupings();
               string[] vars = grouping.Split( new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries );
               //
               foreach ( string item in vars )
               {
                  string[] kvp = item.Split( new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries );
                  if ( kvp[0].ToLower() == "id" )
                     igEntry.id = int.Parse( kvp[1].ToString() );
                  else if ( kvp[0].ToLower() == "name" )
                     igEntry.name = kvp[1].ToString();
                  else if ( kvp[0].ToLower() == "groups" )
                  {
                     if ( kvp.Length > 1 && kvp[1] != null && !string.IsNullOrEmpty( kvp[1].ToString() ) )
                        igEntry.groups = ApiHelper.MakeListFromString( kvp[1].ToString() );
                     else
                        igEntry.groups = new List<string>();
                  }
               }
               igList.Add( igEntry );
            }
            //
            val = igList;
         }
         //
         entry = new KeyValuePair<string, object>( key, val );
         return entry;
      }
   }
}